﻿using System;
using System.Web.UI;
using Microsoft.SharePoint.Utilities;

namespace Eneta.Portal.Forum.Common.Controls
{
    public class OWSDateField : OWSBase
    {
        // Methods
        protected override void Render(HtmlTextWriter wtr)
        {
            wtr.Write("<SCRIPT>fld=new DateField(frm, '{0}', '{1}', '{2}'); fld.fDateOnly = {3}; fld.fHideDescription = {4}; fld.caltype = 1; fld.fRequired = {5}; fld.BuildUI(); </SCRIPT>", new object[] { this.UniqueID, base.Display, base.JavaScriptValue, this.DateOnly.ToString().ToLower(), this.HideDescription.ToString().ToLower(), base.Required.ToString().ToLower() });
            wtr.Write("&nbsp;<br><SPAN class=ms-formdescription ID={0}>{1}</SPAN>", this.UniqueID, this.Description);
        }

        // Properties
        public bool DateOnly
        {
            get
            {
                if (this.ViewState["DateOnly"] == null)
                {
                    this.DateOnly = false;
                }
                return (bool)this.ViewState["DateOnly"];
            }
            set
            {
                this.ViewState["DateOnly"] = value;
            }
        }

        public DateTime DateTime
        {
            get
            {
                return SPUtility.CreateSystemDateTimeFromXmlDataDateTimeFormat(this.Value);
            }
            set
            {
                this.Value = SPUtility.CreateISO8601DateTimeFromSystemDateTime(value);
            }
        }

        public bool HideDescription
        {
            get
            {
                if (this.ViewState["HideDescription"] == null)
                {
                    this.HideDescription = false;
                }
                return (bool)this.ViewState["HideDescription"];
            }
            set
            {
                this.ViewState["HideDescription"] = value;
            }
        }

        public override string Text
        {
            get
            {
                if (this.DateOnly)
                {
                    return this.DateTime.ToString("dddd, MMMM d, yyyy");
                }
                return this.DateTime.ToString("dddd, MMMM d, yyyy h:mm tt");
            }
        }
    }
}
